/*
 * @Author: Colin Dai
 * @Date: 2022-04-30 16:47:05
 * @LastEditors: Colin Dai
 * @LastEditTime: 2022-05-17 19:39:48
 * @FilePath: \intelligent-car\src\wheel_ctrl.ino
 * @Description: 底盘驱动文件
 * dailianzheng@163.com
 * Copyright (c) 2022 by Colin.Dai, All Rights Reserved. 
 */
#include "wheel_ctrl.h"
//设置是否进行调速
#define ADJUST_SPEED


int L1_IN1 = 5;int L1_IN2 = 4;//左前轮 D1,D2
int R1_IN1 = 0;int R1_IN2 = 2;//右前轮 D3,D4

//速度控制针脚
#define PIN_SPEED 13  //D7


/**
 * @brief 初始化底盘
 * @param {*}
 * @return {*}
 */
void init_wheel() 
{ 

  pinMode(L1_IN1, OUTPUT);pinMode(L1_IN2, OUTPUT);
  pinMode(R1_IN1, OUTPUT);pinMode(R1_IN2, OUTPUT);


  pinMode(PIN_SPEED, OUTPUT);

  allstop();
}

void adjustSpeed(int speed)
{  
     analogWrite(PIN_SPEED,speed);
}

/**
 * @brief 左前轮前进
 * @param {*}
 * @return {*}
 */
void L1_forward()
{
  digitalWrite(L1_IN1,HIGH);
  digitalWrite(L1_IN2,LOW); 

 
}

/**
 * @brief 右前轮前进
 * @param {*}
 * @return {*}
 */
void R1_forward()
{
  digitalWrite(R1_IN1,HIGH);
  digitalWrite(R1_IN2,LOW);

}


/**
 * @brief 全部停止
 * @param {*}
 * @return {*}
 */
void allstop()
{
  digitalWrite(L1_IN1,LOW);
  digitalWrite(L1_IN2,LOW);  
  digitalWrite(R1_IN1,LOW);
  digitalWrite(R1_IN2,LOW);
}

/**
 * @brief 左前轮后退
 * @param {*}
 * @return {*}
 */
void L1_backward()
{
  digitalWrite(L1_IN1,LOW);
  digitalWrite(L1_IN2,HIGH);

  
}

/**
 * @brief 右前轮后退
 * @param {*}
 * @return {*}
 */
void R1_backward()//右前轮后退
{
  digitalWrite(R1_IN1,LOW);
  digitalWrite(R1_IN2,HIGH);
}


/**
 * @brief 前进
 * @param {*}
 * @return {*}
 */
void car_forward()
{  
  /*前进*/
  L1_forward();
  R1_forward();
}

/**
 * @brief 后退
 * @param {*}
 * @return {*}
 */
void car_backward()
{
   /*后退*/
  L1_backward();
  R1_backward();  
}

/**
 * @brief 右转
 * @param {*}
 * @return {*}
 */
void car_right()
{  
   /*顺时针原地旋转*/
  L1_forward();
  R1_backward(); 
}

/** 停止
 * @brief 
 * @param {*}
 * @return {*}
 */
void car_stop()
{ 
  allstop();
}

/** 
 * @brief 左转
 * @param {*}
 * @return {*}
 */
void car_left()
{
  L1_backward();
  R1_forward(); 
}

/**测试车轮
 * @param {*}
 * @return {*}
 */
void test_wheel() 
{ 
  /*前进*/
  L1_forward();
  R1_forward();
  
  delay(1500);
  allstop();
  delay(1500);

  /*后退*/
  L1_backward();
  R1_backward();
  delay(1500);
  allstop();
  delay(1500);
  
  /*顺时针原地旋转*/
  L1_forward();
  R1_backward();  
  delay(1500);
  allstop();
  delay(1500);

  /*逆时针原地旋转*/
  L1_backward();
  R1_forward(); 
  delay(1500);
  allstop();
  delay(1500); 
 
}
